#pragma once
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdarg.h>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
#define Info 0
#define Warning 1
#define Error 2
#define fatal 3
#define Debug 4
#define Size 1024

#define Screen 1
#define OneFile 2
#define ClassFile 3
#define LogFile "log.txt"

class Log
{
public:
    Log()
    {
        printMethod = Screen;
        path = "./log/";
    }
    void Eable(int filemode)
    {
        printMethod = filemode;
    }

    string levelToString(int level)
    {
        switch (level)
        {
        case Info:
            return "Info";
            break;
        case Warning:
            return "Warning";
        case Error:
            return "Error";
            break;
        case fatal:
            return "fatal";
        case Debug:
            return "Debug";
        default:
            return "None";
            break;
        }
    }

    // void logmessage(int level, const char *format, ...)
    // {
    //     time_t t = time(NULL);
    //     struct tm *time_t = localtime(&t);

    //     char leftbuffer[Size];
    //     snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d;%d]",
    //              levelToString(level).c_str(), time_t->tm_year + 1900, time_t->tm_mon + 1, time_t->tm_mday,
    //              time_t->tm_hour, time_t->tm_min, time_t->tm_sec);

    //     char rightbuffer[Size];
    //     va_list s;
    //     va_start(s, format);
    //     vsnprintf(rightbuffer, sizeof(rightbuffer), "%s", s);

    //     char log[Size * 2];
    //     snprintf(log, sizeof(log), "%s %s", leftbuffer, rightbuffer);
    //     printLog(level, log);
    // }
    void printLog(int level, const string &Logtext)
    {
        switch (printMethod)
        {
        case Screen:
            cout << Logtext << endl;
            break;
        case OneFile:
            printOneFile(LogFile, Logtext);
            break;
        case ClassFile:
            printClassfile(level, Logtext);
            break;

        default:
            break;
        }
        return;
    }
    void printOneFile(const string &logname, const string &Logtext)
    {
        string _logname = path + logname;
        int fd = open(_logname.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if (fd < 0)
            return;

        write(fd, Logtext.c_str(), Logtext.size());
        close(fd);
    }
    void printClassfile(int level, const string &Logtext)
    {

        string filename = LogFile;
        filename += ".";
        filename += levelToString(level);
        printOneFile(filename, Logtext);
    }
    void operator()(int level, const char *format, ...)
    {
        time_t t = time(NULL);
        struct tm *time_t = localtime(&t);

        char leftbuffer[Size];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d;%d]",
                 levelToString(level).c_str(), time_t->tm_year + 1900, time_t->tm_mon + 1, time_t->tm_mday,
                 time_t->tm_hour, time_t->tm_min, time_t->tm_sec);

        char rightbuffer[Size];
        va_list s;
        va_start(s, format);
        vsnprintf(rightbuffer, sizeof(rightbuffer), "%s", s);

        char log[Size * 2];
        snprintf(log, sizeof(log), "%s %s\nS", leftbuffer, rightbuffer);
        printLog(level, log);
    }
    ~Log()
    {
    }

private:
    int printMethod;
    string path;
     //return 0;
};